import cfg from '../../config';
import {getView} from '../../util/api';
import {formatDate, getCurrDay, randomColor, isArrContain, isArrNoContain, findStr, backPage, getPrevDate, getNextDate, message, closeMsg, getJingTinInfoFun} from '../../util/aid';
import {Slider, sliderHide, sliderShow} from '../../lib/kslider';
import {getSeatParam, setHeadTrianInfo, loadJingTinInfoFun}  from '../../util/common';
import Dialog  from '../../lib/kdialog';
const dialog = new Dialog();

const $ = require('../../lib/kdom');
let _view = require('raw-loader!./list-detail.html');

let _data = null;//父页面的数据
let _opts = null;//父页面的参数

export default class {
  path = '/train/list-detail$';
  hash = '/train/list-detail';
  title = '详情';

  constructor(opt) {
    opt = opt || {};
    this.path = opt.path || this.path;
    this.hash = opt.hash || this.hash;
    this.title = opt.title || this.title;
  }

  // 输出视图
  view(cb) {
    if (!_view) {
      // 静态资源浏览器有缓存,增加时标,强制刷新!
      getView(`${cfg.train.view.listDetail}?t=${(+new Date())}`, '', (rs) => {
        _view = rs;
        cb(null, _view);
      });
    } else
      cb(null, _view);
  }

  // 在已经加载的视图上操作
  bind(dv, params) {
    _data = params.data;
    _opts = params.opts;
    // {from: 'BJP,北京', to: 'SHH,上海', date: '2017-06-22'};
    // 加载动画
    // dialog.loading();

    // 设置头部信息
    setHeadTrianInfo(_data, _opts, trainDetailBlockHead);

    // 经停信息
    $.qu('#pg-train-list-detail .jt-info').onclick = () => {
      getJingTinInfoFun(_data.TrainCode, (rs) => {
        loadJingTinInfoFun(rs);
        sliderShow();
      });
    };


    // loadViewDetail
    loadViewDetail(_data);

    backPage('#pg-train-list-detail', 'backPage', () => {
      $.router.go('/train/list', _opts, true);
    });
  }

  /*bind*/
}

// loadViewDetail
function loadViewDetail(data) {
  if (!data) return;
  let rs = getSeatParam(data);
  let rc = '';
  if (rs) {
    for (let i = 0; i < rs.length; i++) {
      if (rs[i].price && rs[i].seat && rs[i].seatCode && rs[i].yuPiao !== '0') {
        rc += `<li onclick="bookedTickite(this)" data='${JSON.stringify(rs[i])}'>
                <div class="item-content">
                    <div class="item-inner">
                        <div>${rs[i].seat}</div>
                        <div class="text-danger"><span class="f06">￥</span>${rs[i].price}</div>
                        <div class="yuDing">
                            <span class="text-disabled pr075">${rs[i].yuPiao}张</span>
                            <span class="button button-fill button-danger">预定</span>
                        </div>
                    </div>
                </div>
            </li>`;
      }
    }
    if (rc) {
      $.html(listDetailBox, rc);
    } else {
      message('#listDetailBox', '亲！您暂时还没有获取到车次的相关信息哦，请重新查询哦！');
    }
  } else {
    message('#listDetailBox', '亲！您暂时还没有获取到车次的相关信息哦，请重新查询哦！');
  }
  // dialog.close();
}
window.bookedTickite = (that) => {
  $.router.go('/train/book', {data: _data, opts: _opts, currSaet: JSON.parse(that.getAttribute('data')) || null}, true);
};
